What is is-stream?
The is-stream npm package is a simple utility used to check if an object is a Node.js stream. It can differentiate between readable, writable, duplex (both readable and writable), and transform (a type of duplex stream) streams. This package is useful when you need to validate that a given object conforms to the stream interface expected by Node.js APIs.
What are is-stream's main functionalities?
Check if an object is a stream
This feature allows you to check if a given object is a Node.js stream.
const isStream = require('is-stream');
isStream(process.stdin); // true
isStream({}); // false
Check if a stream is readable
This feature allows you to check if a stream is a readable stream.
const isStream = require('is-stream');
isStream.readable(process.stdin); // true
isStream.readable(process.stdout); // false
Check if a stream is writable
This feature allows you to check if a stream is a writable stream.
const isStream = require('is-stream');
isStream.writable(process.stdout); // true
isStream.writable(process.stdin); // false
Check if a stream is a duplex stream
This feature allows you to check if a stream is a duplex stream, which is both readable and writable.
const isStream = require('is-stream');
const { Duplex } = require('stream');
const duplexStream = new Duplex();
isStream.duplex(duplexStream); // true
isStream.duplex(process.stdin); // false
Check if a stream is a transform stream
This feature allows you to check if a stream is a transform stream, which is a type of duplex stream with additional functionality.
const isStream = require('is-stream');
const { Transform } = require('stream');
const transformStream = new Transform();
isStream.transform(transformStream); // true
isStream.transform(process.stdin); // false
Other packages similar to is-stream
isstream
isstream is another package that provides a similar functionality to is-stream. It checks if an object is a stream. The main difference is in the implementation and the specific checks performed. is-stream is more up-to-date and has more downloads, suggesting it is more popular and potentially more reliable.
stream-assert
stream-assert is a package that provides a set of assertion functions for streams, which can be used in testing. While it includes functionality to assert the type of streams, it goes beyond just checking if an object is a stream by offering a suite of assertions for stream content and behavior.
is-stream
Check if something is a Node.js stream
Install
npm install is-stream
Usage
import fs from 'node:fs';
import {isStream} from 'is-stream';
isStream(fs.createReadStream('unicorn.png'));
isStream({});
API
isStream(stream, options?)
Returns a boolean
for whether it's a Stream
.
isWritableStream(stream, options?)
Returns a boolean
for whether it's a stream.Writable
, an http.OutgoingMessage
, an http.ServerResponse
or an http.ClientRequest
.
isReadableStream(stream, options?)
Returns a boolean
for whether it's a stream.Readable
or an http.IncomingMessage
.
isDuplexStream(stream, options?)
Returns a boolean
for whether it's a stream.Duplex
.
isTransformStream(stream, options?)
Returns a boolean
for whether it's a stream.Transform
.
Options
checkOpen
Type: boolean
Default: true
When this option is true
, the method returns false
if the stream has already been closed.
Related